package com.ruoyi.business.service.impl;

import com.ruoyi.business.mapper.TiffSegMapper;
import com.ruoyi.business.utils.OSSUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class TiffUploadService {

    Logger logger = LoggerFactory.getLogger(TiffUploadService.class);

    @Autowired
    private TiffSegMapper tiffSegMapper;

    @Autowired
    private ISysConfigService configService;

    @Value("${oss.tiff_bucket}")
    private String tiff_bucket;

//    @Value("${oss.endpoint}")
//    private String endpoint;
//
//    @Value("${oss.region}")
//    private String region;
//
//    @Value("${oss.accessKeyId}")
//    private String accessKeyId;
//
//    @Value("${oss.accessKeySecret}")
//    private String accessKeySecret;

    @Value("${ruoyi.profile}")
    private String profile;

    @Async("taskExecutor")
    public void processImage(String fileName, Long id, String originalFilename) {
        LocalDate localDate = LocalDate.now();
        String objectName = localDate.getYear() + "/" + localDate.getMonthValue() + "/" + localDate.getDayOfMonth() + "/" + originalFilename;
        try {

            logger.info("开始上传ID为 {} 的文件：{} 到阿里云OSS中", id, fileName);
            // 文件名称
            File file = new File(fileName);
            OSSUtil ossUtil = new OSSUtil(configService.selectConfigByKey("endpoint"), configService.selectConfigByKey("region"), configService.selectConfigByKey("accessKeyId"), configService.selectConfigByKey("accessKeySecret"));
            boolean uploadFile = ossUtil.uploadFile(tiff_bucket, objectName, file);
            if (!uploadFile) {
                logger.error("上传文件失败 fileName:{} id:{}", fileName, id);
                return;
            }
            logger.info("开始上传ID为 {} 的文件：{} 到阿里云OSS中成功", id, fileName);
            // 更新数据库
            tiffSegMapper.updateUploadStatus(id, objectName);

        } catch (Exception e) {
            logger.error("上传文件失败 fileName:{} id:{}", fileName, id, e);
        }













    }
}
